package com.zhengsr.wanandroid.mvp.present;

import com.zhengsr.corelib.bean.ArticleCollectBean;
import com.zhengsr.corelib.bean.BaseResponse;
import com.zhengsr.corelib.bean.KnowTreeBean;
import com.zhengsr.corelib.utils.CommonUtils;
import com.zhengsr.corelib.utils.RxUtils;
import com.zhengsr.wanandroid.mvp.base.BasePresent;
import com.zhengsr.wanandroid.mvp.base.IBaseView;
import com.zhengsr.wanandroid.mvp.contract.ContractManager;
import com.zhengsr.wanandroid.mvp.model.DataManager;
import com.zhengsr.wanandroid.widget.CusSubscribe;

import java.util.List;

/**
 * Created by zhengshaorui
 * Time on 2019/2/25
 */

public class KnowledgePresent extends BasePresent<IBaseView> {
    private IBaseView mView;
    private DataManager mDataManager;
    private int mCurrentPage = 0;
    public static KnowledgePresent create(IBaseView view){
        return new KnowledgePresent(view);
    }
    private KnowledgePresent(IBaseView view){
        mView = view;
        mDataManager = DataManager.getInstance();

        mView = view;
        mView.showLoading();
        if (CommonUtils.isNetworkConnected()){
            getAllKnowTree();
        }else{
            mView.showError();
        }
    }

    public void getAllKnowTree(){
        addSubscribe(mDataManager.getKnowTree()
                .compose(RxUtils.<BaseResponse<List<KnowTreeBean>>>rxScheduers())
                .compose(RxUtils.<List<KnowTreeBean>>handleResult())
                .subscribeWith(new CusSubscribe<List<KnowTreeBean>>(mView) {
                    @Override
                    public void onNext(List<KnowTreeBean> knowTreeBeans) {
                        mView.loadSuccess();
                        if (mView instanceof ContractManager.IKnowTree){
                            ContractManager.IKnowTree iview = (ContractManager.IKnowTree) mView;
                            iview.getAllTree(knowTreeBeans);
                        }

                    }
                })

        );
    }

    public void refreshKnowledge(int cid){
        mCurrentPage = 0;
        getDetailKnowledge(mCurrentPage,cid,true);
    }

    public void loadMoreKnowledge(int cid){
        mCurrentPage ++;
        getDetailKnowledge(mCurrentPage,cid,false);
    }


    /**
     * 获取分类详细文章
     * @param page
     * @param cid
     * @param isRefresh
     */
    private void getDetailKnowledge(int page, int cid, final boolean isRefresh){
        addSubscribe(mDataManager.getDetailKnowledge(page,cid)
                .compose(RxUtils.<BaseResponse<ArticleCollectBean>>rxScheduers())
                .compose(RxUtils.<ArticleCollectBean>handleResult())
                .subscribeWith(new CusSubscribe<ArticleCollectBean>(mView) {
                    @Override
                    public void onNext(ArticleCollectBean articleCollectBean) {
                        mView.loadSuccess();
                        if (mView instanceof ContractManager.IdetailKnowledge){
                            ContractManager.IdetailKnowledge view = (ContractManager.IdetailKnowledge) mView;
                            view.getDetailKnowledge(articleCollectBean,isRefresh);
                        }
                    }
                })
        );
    }

    /**
     * 收藏站内文章
     */
    public void addCollectArticle(int articleId){

        addSubscribe(
                mDataManager.addCollectArticle(articleId)
                        .compose(RxUtils.<BaseResponse<ArticleCollectBean>>rxScheduers())
                        .subscribeWith(new CusSubscribe<BaseResponse<ArticleCollectBean>>(mView) {
                            @Override
                            public void onNext(BaseResponse<ArticleCollectBean> articleCollectBean) {

                                if (mView instanceof ContractManager.IAddOrCancelCollect){
                                    ContractManager.IAddOrCancelCollect view = (ContractManager.IAddOrCancelCollect) mView;
                                    view.addCollectArticleSuccess(articleCollectBean);
                                }
                            }
                        })
        );
    }

    //取消站内文章
    public void cancelCollectArticle(int articleId){

        addSubscribe(
                mDataManager.cancelCollectArticle(articleId)
                        .compose(RxUtils.<BaseResponse<BaseResponse>>rxScheduers())
                        .subscribeWith(new CusSubscribe<BaseResponse<BaseResponse>>(mView) {
                            @Override
                            public void onNext(BaseResponse<BaseResponse> baseResponseBaseResponse) {
                                if (mView instanceof ContractManager.IAddOrCancelCollect){
                                    ContractManager.IAddOrCancelCollect view = (ContractManager.IAddOrCancelCollect) mView;
                                    view.cancelCollectArticleSuccess(baseResponseBaseResponse);
                                }
                            }
                        })
        );
    }
}
